% Copyright (c) 2012, Suraj Gowda 
% All rights reserved.
% 
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%     * Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%     * Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in the
%       documentation and/or other materials provided with the distribution.
%     * Neither the name of the <organization> nor the
%       names of its contributors may be used to endorse or promote products
%       derived from this software without specific prior written permission.
% 
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL Suraj Gowda BE LIABLE FOR ANY
% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function insert_struct_into_table(tbl_name, s, varargin)

defaults = {...
    'datatypes_consistent', 0, ...
    'max_str_len', 100, ...
};

datatypes_consistent = get_var('datatypes_consistent', 'defaults', defaults, varargin{:});
max_str_len = get_var('max_str_len', 'defaults', defaults, varargin{:});

    col_query = mym(['show columns from ', tbl_name]);
    col_names = col_query.Field';
    col_names = col_names(1, 2:end);% Always assume first column is ID

    % keep only the database col names that are also struct fields
    struct_fields = fieldnames(s);
    common_fields = find(ismember( col_names, struct_fields ));

    col_names_common = {};
    for k = 1:length(common_fields):
        field_idx = common_fields(k);
        col_names_common{k} = col_names{field_idx};
    end
    col_names = col_names_common;
        
    cols = '';
    col_data_types = '';
    data_values = {};
    for k =1:length(col_names)

        data_values{k} = s.(col_names{k});
        if isstr(data_values{k}) && datatypes_consistent
            x = '"{S}"';
        else
            x = '"{M}"';
        end
            

        if k < length(col_names)
            connector = ', ';
        else
            connector = '';
        end

        col_data_types = [col_data_types, x, connector];
        cols = [ cols, col_names{k}, connector];
    end

    mym_cmd = sprintf('insert into %s (%s) values (%s)', tbl_name, cols, col_data_types) 
    mym(mym_cmd, data_values{:}); 
end


